<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               :row-class-name="rowStyle"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad"
    >
      <template slot="menuRight" slot-scope="{size}">
        <el-button type="primary" :size="size" @click="dowloadFile">导出订单</el-button>
      </template>
      <template slot="expand" slot-scope="{row}">
        <div class="table-expand-box">
          <div class="table-expand-box-order">
            <div class="order-img"></div>
            <div class="order-box">
              <p class="box-title">{{ row.openMobilePkgMenu }}</p>
              <p>{{ row.openMobile }}</p>
              <p>{{ row.openMobileProvinceName }}省{{ row.openMobileCityName }}移动</p>
            </div>
          </div>
          <div class="table-expand-box-owner">
            <p class="box-title">{{ row.ownerName }}</p>
            <p>{{ row.ownerMobile }}</p>
            <p>{{ row.ownerIdentityId }}</p>
            <p>{{ row.ownerAddress }}</p>
          </div>
          <div class="table-expand-box-orderStatus">
            <span>{{ dicMap[row.deliverStatus] }}</span>
          </div>
          <div class="table-expand-box-menu">
            <el-button type="text" @click="showDetail(row.id)">查看</el-button>
          </div>
        </div>
      </template>
      <template slot-scope="{row}" slot="order">
        <div>订单编号：{{ row.orderNo }}</div>
      </template>
      <template slot-scope="{row}" slot="owner">
        <div>下单时间：{{ row.createTime }}</div>
      </template>
      <template slot-scope="{row}" slot="orderStatus">
        <div>商户：{{ row.merchantName }}</div>
      </template>
      <template slot-scope="{row}" slot="checkStatus">
      </template>
    </avue-crud>
    <el-drawer
      title="订单详情"
      :append-to-body="true"
      :visible.sync="isShowDetail"
      direction="rtl"
      size="960px">
      <div class="drawe-top">
        <div class="drawe-top-left">
          <div class="drawe-top-left-title">订单编号:{{ drawerData.orderNo }}</div>
          <div class="drawe-top-left-content">
            <div>
              来源商户：{{ drawerData.merchantName }}
            </div>
            <div>
              下单时间：{{ drawerData.createTime }}
            </div>
          </div>
        </div>
        <div class="drawe-top-right">
          <div>状态</div>
          <div>{{ dicMap[drawerData.deliverStatus] }}</div>
          <div>更新时间：{{ drawerData.updateTime ? drawerData.updateTime : drawerData.createTime }}</div>
        </div>
      </div>
      <div class="drawe-bottom">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>办理人信息</span>
          </div>
          <div>
            <div><span>姓名：</span>{{ drawerData.ownerName }}</div>
            <div><span>手机号：</span>{{ drawerData.ownerMobile }}</div>
            <div><span>身份证号：</span>{{ drawerData.ownerIdentityId }}</div>
            <div><span>地址：</span>{{ drawerData.ownerAddress }}</div>
          </div>
        </el-card>
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>号码信息</span>
          </div>
          <div><span>号码：</span>{{ drawerData.openMobile }}</div>
          <div><span>归属地：</span>{{ drawerData.openMobileProvinceName }}省{{ drawerData.openMobileCityName }}移动</div>
          <div><span>商品套餐：</span>{{ drawerData.openMobilePkgMenu }}|{{ drawerData.pkgRealPrice }}元</div>
          <div><span>商品编码：</span>{{ drawerData.packageCode }}</div>
          <div><span>号码价格：</span>{{ drawerData.openPrettyPrice == -1 ? '0.00' : drawerData.openPrettyPrice }}元</div>
          <div><span>状态：</span>{{ drawerData.openDate ? "已激活" : "未激活" }}</div>
        </el-card>
      </div>
    </el-drawer>
  </basic-container>
</template>

<script>
import {getList, getDetail, add, update, remove, getDic, getFile} from "@/api/commision/openpackageorder";
import {mapGetters} from "vuex";

export default {
  data() {
    return {
      form: {},
      query: {
        // createTimeStart:"",
        // createTimeEnd:""
      },
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      dicMap: {},
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 30,
        tip: false,
        searchShow: false,
        // searchMenuSpan:8,
        searchLabelWidth: 120,
        // searchIndex:1,
        // searchIcon:true,
        // searchShowBtn:false,
        viewBtn: true,
        refreshBtn: false,
        columnBtn: false,
        dialogClickModal: false,
        menu: false,
        expand: true,
        defaultExpandAll: true,
        expandWidth: 1,
        // column: [
        //   {
        //     label: "订单编号",
        //     prop: "orderNo",
        //     rules: [{
        //       required: true,
        //       message: "请输入订单编号",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "批次号",
        //     prop: "bthNo",
        //     rules: [{
        //       required: true,
        //       message: "请输入批次号",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "渠道订单号",
        //     prop: "chanOrderNo",
        //     rules: [{
        //       required: true,
        //       message: "请输入渠道订单号",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "商户id",
        //     prop: "merchantId",
        //     rules: [{
        //       required: true,
        //       message: "请输入商户id",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "商户名称",
        //     prop: "merchantName",
        //     rules: [{
        //       required: true,
        //       message: "请输入商户名称",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "机主名字",
        //     prop: "ownerName",
        //     rules: [{
        //       required: true,
        //       message: "请输入机主名字",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "机主手机号",
        //     prop: "ownerMobile",
        //     rules: [{
        //       required: true,
        //       message: "请输入机主手机号",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "机主身份证号",
        //     prop: "ownerIdentityId",
        //     rules: [{
        //       required: true,
        //       message: "请输入机主身份证号",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "机主详细地址",
        //     prop: "ownerAddress",
        //     rules: [{
        //       required: true,
        //       message: "请输入机主详细地址",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "机主所在省编码",
        //     prop: "ownerProvinceCode",
        //     rules: [{
        //       required: true,
        //       message: "请输入机主所在省编码",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "机主所在市编码",
        //     prop: "ownerCityCode",
        //     rules: [{
        //       required: true,
        //       message: "请输入机主所在市编码",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "机主所在区或者县编码",
        //     prop: "ownerTownCode",
        //     rules: [{
        //       required: true,
        //       message: "请输入机主所在区或者县编码",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "开通手机号;所选手机号",
        //     prop: "openMobile",
        //     rules: [{
        //       required: true,
        //       message: "请输入开通手机号;所选手机号",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "开通靓号价格",
        //     prop: "openPrettyPrice",
        //     rules: [{
        //       required: true,
        //       message: "请输入开通靓号价格",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "开通手机号省级编码",
        //     prop: "openMobileProvinceCode",
        //     rules: [{
        //       required: true,
        //       message: "请输入开通手机号省级编码",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "开通手机号市级编码",
        //     prop: "openMobileCityCode",
        //     rules: [{
        //       required: true,
        //       message: "请输入开通手机号市级编码",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "开通手机号区级编码",
        //     prop: "openMobileTownCode",
        //     rules: [{
        //       required: true,
        //       message: "请输入开通手机号区级编码",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "开通手机号套餐",
        //     prop: "openMobilePkgMenu",
        //     rules: [{
        //       required: true,
        //       message: "请输入开通手机号套餐",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "套餐原价",
        //     prop: "pkgOriPrice",
        //     rules: [{
        //       required: true,
        //       message: "请输入套餐原价",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "套餐办理价格",
        //     prop: "pkgRealPrice",
        //     rules: [{
        //       required: true,
        //       message: "请输入套餐办理价格",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "订单状态",
        //     prop: "orderStatus",
        //     rules: [{
        //       required: true,
        //       message: "请输入订单状态",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "发货状态",
        //     prop: "deliverStatus",
        //     rules: [{
        //       required: true,
        //       message: "请输入发货状态",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "审核状态，0为未审核，1为审核",
        //     prop: "checkStatus",
        //     rules: [{
        //       required: true,
        //       message: "请输入审核状态，0为未审核，1为审核",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "资费套餐编码",
        //     prop: "packageCode",
        //     rules: [{
        //       required: true,
        //       message: "请输入资费套餐编码",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "开卡激活日期",
        //     prop: "openDate",
        //     rules: [{
        //       required: true,
        //       message: "请输入开卡激活日期",
        //       trigger: "blur"
        //     }]
        //   },
        //   {
        //     label: "物流订单号",
        //     prop: "expressOrderNo",
        //     rules: [{
        //       required: true,
        //       message: "请输入物流订单号",
        //       trigger: "blur"
        //     }]
        //   },
        // ]
        column: [
          {
            label: "订单编号",
            prop: "orderNo",
            search: true,
            hide: true,
            display: false,
          },
          {
            label: "机主名字",
            prop: "ownerName",
            search: true,
            hide: true,
            display: false,
          },
          {
            label: "机主手机号",
            prop: "ownerMobile",
            search: true,
            hide: true,
            display: false,
          },
          {
            label: "机主身份证号",
            prop: "ownerIdentityId",
            search: true,
            hide: true,
            display: false,
          },
          {
            label: "下单时间",
            prop: "createTime",
            search: true,
            searchSpan: 12,
            searchRange: true,
            hide: true,
            display: false,
            type: "date",
            minWidth: 180,
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            format: "yyyy-MM-dd",
            defaultTime: ['00:00:00', '23:59:59'],
            // searchValue:[new Date(),new Date()],
            pickerOptions: {
              shortcuts: [{
                text: '今天',
                onClick(picker) {
                  const end = new Date();
                  const start = new Date();
                  // start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                  picker.$emit('pick', [start, end]);
                }
              }, {
                text: '昨天',
                onClick(picker) {
                  const end = new Date();
                  const start = new Date();
                  start.setTime(start.getTime() - 3600 * 1000 * 24);
                  end.setTime(end.getTime() - 3600 * 1000 * 24);
                  picker.$emit('pick', [start, end]);
                }
              }, {
                text: '7天内',
                onClick(picker) {
                  const end = new Date();
                  const start = new Date();
                  start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                  picker.$emit('pick', [start, end]);
                }
              }, {
                text: '30天内',
                onClick(picker) {
                  const end = new Date();
                  const start = new Date();
                  start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                  picker.$emit('pick', [start, end]);
                }
              }]
            }
          },
          {
            label: "商户",
            prop: "merchantId",
            type: "select",
            dicUrl: "/api/merchant-bmg/merchant/list?current=1&size=100",
            dicMethod: 'get',
            props: {
              label: "merchantName",
              value: "id",
              res: "data.records"
            },
            search: true,
            hide: true,
            display: false,
          },
          {
            label: "办理地址",
            prop: "ownerAddress",
            search: true,
            hide: true,
            display: false,
          },
          {
            label: "产品信息",
            prop: "order",
            width: 400,
          },
          {
            label: "办理人信息",
            prop: "owner",
            width: 400,
          },
          {
            label: "订单状态",
            prop: "orderStatus",
            type: "select",
            // search:true,
            display: false,
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=COMMISION_ORDER_STATUS",
            dicMethod: 'get',
            props: {
              label: "dictValue",
              value: "dictKey"
            },
          },
          {
            label: "发货状态",
            prop: "deliverStatus",
            type: "select",
            search: true,
            searchLabel: "订单状态",
            hide: true,
            display: false,
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=COMMISION_DELIVER_STATUS",
            dicMethod: 'get',
            props: {
              label: "dictValue",
              value: "dictKey"
            },
          },
          {
            label: "操作",
            prop: "checkStatus",
            align: "center",
          },
        ]
      },
      data: [],
      drawerData: {},
      isShowDetail: false,
    };
  },
  created() {
    getDic("COMMISION_DELIVER_STATUS").then((res) => {
      res.data.data.forEach((item) => {
        this.dicMap[item.dictKey] = item.dictValue
      })
    });
    // this.initDate()
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.openpackageorder_add, false),
        viewBtn: this.vaildData(this.permission.openpackageorder_view, false),
        delBtn: this.vaildData(this.permission.openpackageorder_delete, false),
        editBtn: this.vaildData(this.permission.openpackageorder_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    initDate() {
      this.query.createTimeStart = this.funInitTime();
      this.query.createTimeEnd = this.funInitTime();
    },
    // 获取当前日期  前几天 初始化时间
    funInitTime(p_count = 0) {
      // alert(0)
      var dd = new Date();
      dd.setDate(dd.getDate() + p_count); //获取p_count天后的日期
      var y = dd.getFullYear();
      var m = dd.getMonth() + 1; //获取当前月份的日期
      if (m < 10) {
        m = "0" + m;
      }
      var d = dd.getDate();
      if (d < 10) {
        d = "0" + d;
      }
      return y + "-" + m + "-" + d;
    },
    rowSave(row, done, loading) {
      add(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        loading();
        window.console.log(error);
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        loading();
        console.log(error);
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      if (params.createTime) {
        params.createTimeStart = params.createTime[0]
        params.createTimeEnd = params.createTime[1]
        delete params.createTime
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    rowStyle() {
      return "little-row"
    },
    showDetail(id) {
      getDetail(id).then(
        (res) => {
          this.isShowDetail = true;
          this.drawerData = res.data.data
        }, () => {

        }
      )
    },
    dowloadFile() {
      getFile(this.query).then(res => {
        var blob = new Blob([res.data]); //创建一个blob对象
        var a = document.createElement('a'); //创建一个<a></a>标签
        a.href = URL.createObjectURL(blob); // response is a blob
        a.download = "号卡订单.xlsx";  //文件名称
        a.style.display = 'none';
        document.body.appendChild(a);
        a.click();
        a.remove();
      })
    }
  }
};
</script>

<style lang="scss" scoped>
:deep(.el-table__header-wrapper) {
  margin-bottom: 10px;
}

:deep(.el-table__expanded-cell) {
  padding: 0 !important;
}

:deep(.el-table__body-wrapper .little-row) {
  padding: 5px 0;

  td {
    background: #f2f2f2;
    padding: 0;
  }
}

:deep( .table-expand-box) {
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: 100px;

  .table-expand-box-order {
    display: flex;
    width: 400px;
    align-items: center;
    padding: 0 10px;

    .order-img {
      width: 70px;
      height: 70px;
      background: #999;
    }

    .order-box {
      display: flex;
      flex-direction: column;
      justify-content: center;
      padding: 0 10px;

      p {
        margin: 2px 0;
        line-height: 16px;

        &.box-title {
          font-size: 16px;
          line-height: 18px;
          color: #000;
        }
      }
    }
  }

  .table-expand-box-owner {
    width: 400px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 0 10px;

    p {
      margin: 2px 0;
      line-height: 16px;

      &.box-title {
        font-size: 16px;
        line-height: 18px;
        color: #000;
      }
    }
  }

  .table-expand-box-orderStatus {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .table-expand-box-menu {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

:deep( .el-drawer__body) {
  display: flex;
  flex-direction: column;
  font-size: 13px;
}

:deep( .drawe-top) {
  width: 100%;
  box-sizing: border-box;
  padding: 20px 30px;
  height: 150px;
  display: flex;

  .drawe-top-left {
    width: 50%;

    .drawe-top-left-title {
      font-weight: 700;
      font-size: 20px;
      color: #000000d8;
      line-height: 28px;
    }

    .drawe-top-left-content {
      margin-top: 40px;
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }

  .drawe-top-right {
    width: 50%;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    align-items: flex-end;

    & > div {
      font-size: 14px;
      color: #00000072;
      line-height: 22px;
    }

    & > div:nth-child(2) {
      font-size: 20px;
      color: #000000d8;
      line-height: 28px;
    }
  }
}

:deep( .drawe-bottom) {
  flex: 1;
  padding: 40px;
  display: flex;
  justify-content: space-around;
  background: #f0f2f5;

  .box-card {
    width: 45%;
    height: 250px;

    .el-card__header {
      font-size: 14px;
      color: #000;
      font-weight: 600;
    }

    .el-card__body {
      padding-left: 40px;

      div {
        line-height: 30px;
        color: #00000072;

        span {
          width: 80px;
          display: inline-block;
          text-align: right;
          color: #00000096
        }
      }


    }
  }
}
</style>
